package com.example.ddz.framework.impl;

/**
 * Created by IntelliJ IDEA.
 * User: Marcel Melzig
 * Date: 02.08.11
 * Time: 19:45
 */

import java.util.List;

import android.content.Context;
import android.os.Build.VERSION;
import android.view.View;

import com.example.ddz.framework.IInput;
import com.example.ddz.framework.ITouchHandler;

/**
 * This class manage all other handlers of the input framework.
 */
public class AndroidInput implements IInput
{
    private final AccelerometerHandler accelHandler;
    private final KeyboardHandler keyHandler;
    private final ITouchHandler touchHandler;

    /**
     * Creates an instance of this class.
     *
     * @param context The context of the application.
     * @param view    The view that fires the input events.
     */
    public AndroidInput(Context context, View view)
    {
        accelHandler = new AccelerometerHandler(context);
        keyHandler = new KeyboardHandler(view);
        if (Integer.parseInt(VERSION.SDK) < 5)
            touchHandler = new SingleTouchHandler(view);
        else
            touchHandler = new MultiTouchHandler(view);
    }

    /**
     * Passes the event to the handler.
     *
     * @param keyCode The code of the pressed key.
     * @return True if the key is pressed, otherwise false.
     */
    @Override
    public boolean IsKeyPressed(int keyCode)
    {
        return keyHandler.IsKeyPressed(keyCode);
    }

    /**
     * Passes the event to the handler.
     *
     * @param pointer The pointer of the touch event.
     * @return True if the given pointer is touched, otherwise false.
     */
    @Override
    public boolean IsTouchDown(int pointer)
    {
        return touchHandler.IsTouchDown(pointer);
    }

    /**
     * Gets the x value of the pointer.
     *
     * @param pointer The pointer of the touch event.
     * @return The x value of the pointer.
     */
    @Override
    public int GetTouchX(int pointer)
    {
        return touchHandler.GetTouchX(pointer);
    }

    /**
     * Gets the y value of the pointer.
     *
     * @param pointer The pointer of the touch event.
     * @return The y value of the pointer.
     */
    @Override
    public int GetTouchY(int pointer)
    {
        return touchHandler.GetTouchY(pointer);
    }

    /**
     * Gets the acceleration in x direction.
     *
     * @return The acceleration in x direction.
     */
    @Override
    public float GetAccelX()
    {
        return accelHandler.GetAccelX();
    }

    /**
     * Gets the acceleration on y axis.
     *
     * @return The acceleration in y axis.
     */
    @Override
    public float GetAccelY()
    {
        return accelHandler.GetAccelY();
    }

    /**
     * Gets the acceleration on z axis.
     *
     * @return The accereration on z axis.
     */
    @Override
    public float GetAccelZ()
    {
        return accelHandler.GetAccelZ();
    }

    /**
     * Gets a list pf touch events handled by the handler.
     *
     * @return A list of the last touch events.
     */
    @Override
    public List<TouchEvent> GetTouchEvents()
    {
        return touchHandler.GetTouchEvents();
    }

    /**
     * Gets a list of the last key events.
     *
     * @return A list of the last key events.
     */
    @Override
    public List<KeyEvent> GetKeyEvents()
    {
        return keyHandler.getKeyEvents();
    }
}
